<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class lock_list extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";
          $this->session->set_userdata(array('Url'=>uri_string()));
		  //load language
		  $this->lang->load('lock_list');
          $this->load->model('lock_list_model','lock_list');
      }   
	  function index(){
		  $this->acl->check('view','mod_access','lock_list',base_url());
		  $this->list_lock_list();
	  }
      function list_lock_list(){
         	$this->acl->check('view','mod_access','lock_list',base_url());
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('notice'=>'Ghi chú'),'lock_list/list_lock_list/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            $data['title'] = 'Danh sách địa chỉ IP';
			if($this->acl->check('view','mod_access','lock_list'))
	            $data['add'] = 'lock_list/add';
            $config['base_url'] = base_url().'lock_list/list_lock_list/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->lock_list->get_num_data($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['list'] =   $this->lock_list->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            $data['pagination']    = $this->pagination->create_links();            
            $this->_templates['page'] = 'lock_list/list_lock_list';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
      function add(){   
	  		$this->acl->check('add','mod_access','lock_list',base_url());                      
			$data['title'] = 'Thêm mới địa chỉ ip';
			$this->form_validation->set_rules('ip',lang('lock_list_ip'),'trim|required');
			$this->form_validation->set_rules('notice',lang('lock_list_notice'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 $data = $this->build_data($_POST,1);
                 if($this->lock_list->saveData(0,$data)){
                     $this->session->set_flashdata('message','Lưu thành công');
                     redirect('lock_list/list_lock_list');
                 }else{
                     $this->pre_message = 'Lưu không thành công';
                 }
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'lock_list/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function edit($id){
		  $this->acl->check('edit','mod_access','lock_list',base_url());                
          $data['title'] = 'Sửa câu hỏi';
          $data['query'] = $this->lock_list->get_object($id);
          $this->form_validation->set_rules('ip',lang('lock_list_ip'),'trim|required');
		  $this->form_validation->set_rules('notice',lang('lock_list_notice'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 $data = $this->build_data($_POST,1);
                 if($this->lock_list->saveData($id,$data)){
                     $this->session->set_flashdata('message','Lưu thành công');
                     redirect('lock_list/list_lock_list');
                 }else{
                     $this->pre_message = 'Lưu không thành công';
                 }
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'lock_list/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
		$this->acl->check('del','mod_access','lock_list',base_url());
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->lock_list->deleted($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('lock_list/list_lock_list/'.$page);
    }
    function dels(){
		$this->acl->check('dels','mod_access','lock_list',base_url());
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->lock_list->deleted($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('lock_list/list_lock_list/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
		$dataUpdate["ip"]=trim($data["ip"]);
		$dataUpdate["notice"]=trim($data["notice"]);
		if($flag==1)
           $dataUpdate["dt_create"] = date('Y-m-d H:i:s');
		if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}
    
  }
?>
